<template>
  <div class="dashboard-container">
    <aside class="aside-style">
      <el-row>
        <el-col :span="9">
          <el-date-picker
            v-model="datetime"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            :default-time="['00:00:00', '23:59:59']"
            value-format="yyyy-MM-dd"
            clearable
            @change="dateChange"
          />
        </el-col>
        <el-col :span="5">
          <el-select v-model="department_id" clearable placeholder="请选择部门" @change="departmentChange">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.department"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="5">
          <el-select
            v-model="user_id"
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="请输入用户名"
            :remote-method="getUserList"
            :loading="loading"
            @change="userChange"
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.user_name"
              :value="item.id"
            >
              <span style="float: left">{{ item.user_name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.department }}</span>
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </aside>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top:20px; font-size:12px;"
    >
      <el-table-column
        prop="order_num"
        label="订单"
        width="160"
        align="center"
      />
      <el-table-column
        prop="department"
        label="发起"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.user_name }}
          <p> <strong>{{ scope.row.department }}</strong></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="job_name"
        label="职位"
        align="center"
      />
      <el-table-column
        prop="pay"
        label="薪资"
        width="70"
        align="center"
      />
      <el-table-column
        prop="edu"
        label="学历"
        width="100"
        align="center"
      />
      <el-table-column
        prop="job_bg"
        label="工作经历"
        width="75"
        align="center"
      />
      <el-table-column
        prop="job_nature"
        label="工作性质"
        width="75"
      />
      <el-table-column
        prop="gender"
        label="性别"
        width="50"
        align="center"
      />
      <el-table-column
        prop="create_time"
        label="发起时间"
        align="center"
      />
      <el-table-column
        label="操作"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="checkOrderInfo(scope.row)">查看</el-button>
          <el-button v-if="scope.row.confirm_status === '301'" type="text" size="mini" @click="completeRecruitFun(scope.row)">完成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" @pagination="paginationFun" />
  </div>
</template>

<script>
import { getRecruitListApi, getDepartmentListApi, getUserListByNameApi, completeRecruitApi } from '@/api/main'
import pagination from '@/components/Pagination'
export default {
  components: {
    pagination
  },
  data() {
    return {
      userList: [],
      list: [],
      departmentList: [],
      p: 1,
      limit: 10,
      total: 0,
      datetime: '',
      start_time: '',
      end_time: '',
      user_id: '',
      loading: false,
      department_id: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.getRecruitList()
    this.getDepartmentList()
  },
  methods: {
    completeRecruitFun(row) {
      this.$confirm('是否确认编号为： ' + row.order_num + ' 的招聘订单已完成？', '确认信息', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      })
        .then(() => {
          const data = {
            order_num: row.order_num
          }
          completeRecruitApi(data).then(res => {
            if (res.code === 200) {
              this.$message.success('信息提交成功，待部门确认！')
              this.getRecruitList()
            }
          })
        })
        .catch(action => {

        })
    },
    checkOrderInfo(row) {
      this.$router.push({
        name: 'recruit_info',
        query: { order_num: row.order_num }
      })
    },
    getUserList(query) {
      this.loading = true
      console.log(query)
      const data = {
        user_name: query
      }
      getUserListByNameApi(data).then(res => {
        if (res.code === 200) {
          this.userList = res.user
          this.loading = false
        }
      })
    },
    paginationFun(val) {
      this.p = val.page
      this.limit = val.limit
      this.getRecruitList()
    },
    reloadFun() {
      this.getUserList()
    },
    userChange() {
      this.getRecruitList()
    },
    getDepartmentList() {
      getDepartmentListApi().then(res => {
        if (res.code === 200) {
          this.departmentList = res.list
        }
      })
    },
    departmentChange(val) {
      this.department_id = val
      this.getRecruitList()
    },
    dateChange(val) {
      if (val) {
        this.start_time = val[0] + ' 00:00:00'
        this.end_time = val[1] + ' 23:59:59'
      } else {
        this.start_time = ''
        this.end_time = ''
      }
      this.getRecruitList()
    },
    getRecruitList() {
      const data = {
        status: 3,
        p: this.p,
        limit: this.limit,
        start_time: this.start_time,
        end_time: this.end_time,
        user_id: this.user_id,
        department_id: this.department_id
      }
      getRecruitListApi(data).then(res => {
        this.list = res.list
        this.total = Number(res.count)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
    &-container {
        margin: 30px;
    }
    &-text {
        font-size: 30px;
        line-height: 46px;
    }
}
</style>
